package cn.sms.pet.controller;

import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.util.AjaxResult;
import cn.sms.basic.util.PageList;
import cn.sms.pet.domain.Pet;
import cn.sms.pet.query.PetQuery;
import cn.sms.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable Long id){
        try {
            petService.adopt(id);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @GetMapping("/{petId}")
    public Pet getPet(@PathVariable("petId") Long petId){
        return petService.queryByIdDetail(petId);
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }



    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }
}
